package com.JZ.QM.service.impl;

import com.JZ.QM.common.dao.ComEarningDetailDao;
import com.JZ.QM.common.dao.ComEarningRecordDao;
import com.JZ.QM.common.dao.PositionDetailDao;
import com.JZ.QM.common.dao.WealthAccountDao;
import com.JZ.QM.common.entity.*;
import com.JZ.QM.common.utils.PageUtils;
import com.JZ.QM.common.utils.Query;
import com.JZ.QM.service.ComEarningRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author: cyc
 * @email cyc@gmail.com
 * @date: 2020/7/8 14:05
 */
@Service("comEarningRecord")
public class ComEarningRecordServiceImpl extends ServiceImpl<ComEarningRecordDao, ComEarningRecordEntity> implements ComEarningRecordService {

    @Resource
    ComEarningDetailDao comEarningDetailDao;
    @Resource
    ComEarningRecordDao comEarningRecordDao;
    @Resource
    WealthAccountDao wealthAccountDao;
    @Resource
    PositionDetailDao positionDetailDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) throws Exception {
        IPage<ComEarningRecordEntity> page = this.page(
                new Query<ComEarningRecordEntity>().getPage(params),
                new QueryWrapper<ComEarningRecordEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Boolean caculateComEarning() {
        //条件构造器
        QueryWrapper queryWrapperCom=new QueryWrapper();
        //
        QueryWrapper queryWrapperWea = new QueryWrapper();
        //组合基本信息列表
        ArrayList<ComEarningDetailEntity> comEarningDetailEntities = new  ArrayList();
        //组合基本信息
        ComEarningDetailEntity comEarningDetailEntity = new ComEarningDetailEntity();
        WealthAccountEntity wealthAccountEntity=new WealthAccountEntity();
        ComEarningRecordEntity comEarningRecordEntity = new ComEarningRecordEntity();
        //定义时间的格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");//设置日期格式
        //定义日历时间
        Calendar c = Calendar.getInstance();
        //前一天时间
        Date start=new Date();
        //前一天日期格式转换
        String combinationUpdateDate="";
        BigDecimal coEarningRadio=new BigDecimal(0);

        //取得前一天日期
        c.add(Calendar.DATE, -1);
        start = c.getTime();
        combinationUpdateDate = dateFormat.format(start);
        //获取组合收益详细信息列表
        comEarningDetailEntities= comEarningDetailDao.getComEarningDetailList(combinationUpdateDate);
        System.out.println("组合信息为"+comEarningDetailEntities);
        //遍历组合的收益信息列表
        for (Iterator<ComEarningDetailEntity> it = comEarningDetailEntities.iterator(); it.hasNext(); ) {
            comEarningDetailEntity= it.next();
            if(comEarningDetailEntity.getWealthAccountAmoount().compareTo(new BigDecimal(0))!=0){
                coEarningRadio=comEarningDetailEntity.getEntrustAmount().divide(comEarningDetailEntity.getWealthAccountAmoount(),5, BigDecimal.ROUND_HALF_UP);
            } else {
                coEarningRadio=new BigDecimal(0);
            }
            //创建组合收益信息实体
            comEarningRecordEntity.setWealthAccountId(comEarningDetailEntity.getWealthAccountId());
            comEarningRecordEntity.setComEarningId(comEarningDetailEntity.getComEarningId());
            comEarningRecordEntity.setEntrustAmount(comEarningDetailEntity.getEntrustAmount());
            comEarningRecordEntity.setReturnRate(coEarningRadio);
            comEarningRecordEntity.setTradeDate(combinationUpdateDate);
            comEarningRecordEntity.setUserId(comEarningDetailEntity.getUserId());
            //插入组合收益表中
            comEarningRecordDao.insert(comEarningRecordEntity);
            //创建财富账号实体
            wealthAccountEntity.setWealthAccountId(comEarningDetailEntity.getWealthAccountId());
            wealthAccountEntity.setWealthAccountAmoount(comEarningDetailEntity.getWealthAccountAmoount().add(comEarningDetailEntity.getEntrustAmount()));
            //更新财富账号金额
            wealthAccountDao.updateById(wealthAccountEntity);

        }
        return true;
    }

    /**
     * 获取用户购买后的组合比例
     * @param comBaseId 组合id
     * @return
     */
    @Override
    public ArrayList<PositionDetailEntity> getComPositionDetailItems(Long comBaseId) {
        //原组合累积持仓信息
        ArrayList<PositionDetailEntity> positionDetailEntities=new ArrayList<>();

        positionDetailEntities=positionDetailDao.getComPositionDetailItems(comBaseId);
        return positionDetailEntities;
    }

    /**
     * 展示组合收益信息记录
     * @param accoutId 账户id
     * @return
     */
    @Override
    public HashMap showComEarningRecordForChart(Long accoutId) {
        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //组合收益记录信息列表
        ArrayList<ComEarningRecordEntity> comEarningRecordEntities=new ArrayList<>();
        //组合收益信息
        ComEarningRecordEntity comEarningRecordEntity=new ComEarningRecordEntity();
        //组合的日收益金额
        BigDecimal[] comDailyEarning;
        //组合的日收益率
        BigDecimal[] comDailyEarningRadio;
        //组合的交易日期
        String[] dailyTradeDate;
        //结果集
        HashMap result=new HashMap();
        //交易的天数
        int size=0;
        //数组索引
        int index=0;

        //条件构造器，查询组合的收益金额，收益率，与交易日期
        queryWrapper.select("return_rate","entrust_amount","trade_date")
                .eq("wealth_account_id",accoutId);
        queryWrapper.orderByAsc("trade_date");
        //组合持仓收益信息
        comEarningRecordEntities= (ArrayList<ComEarningRecordEntity>) comEarningRecordDao.selectList(queryWrapper);
        //初始化数组
        size=comEarningRecordEntities.size();
        comDailyEarning=new BigDecimal[size];
        comDailyEarningRadio=new BigDecimal[size];
        dailyTradeDate=new String[size];

        //获取收益信息
        for (Iterator<ComEarningRecordEntity> it = comEarningRecordEntities.iterator(); it.hasNext(); ) {
            comEarningRecordEntity = it.next();
            comDailyEarning[index]=comEarningRecordEntity.getEntrustAmount();
            comDailyEarningRadio[index]=comEarningRecordEntity.getReturnRate();
            dailyTradeDate[index]=comEarningRecordEntity.getTradeDate();
            index++;
        }
        //封装结果集
        result.put("tradeDate",dailyTradeDate);
        result.put("comEarning",comDailyEarning);
        result.put("comEarningRadio",comDailyEarningRadio);
        return result;

    }
}
